<?php
namespace app\index\controller;
use think\view;
use think\Db;
use think\Session;
use Qiniu\Auth as Auth;
use think\Cache;
use traits\model\SoftDelete;

class Documentation extends CommonController
{

    function show($status,$message,$data=array()){
        $reuslt = array(
            'status'=>$status,
            'message'=>$message,
            'data'=>$data,
        );
        exit(json_encode($reuslt)) ;   //输出一条信息 并退出当前脚本
    }
    /**
     * @return mixed
     * 案例管理
     */
    public function index()
    {
        return $this->fetch();
    }
    /**
     * 新增案例
     */
    public function add(){
        return $this->fetch();
    }

    /**
     * 编辑案例
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(){
        if($_GET){
            $map['id']  = $_GET['id'];
            $list = Db::name('list')->where($map)->find();
            $this->assign('list',$list);
        }
//        提交更新内容
        if($_POST){
            $id  = $_POST['id'];
            $data['title'] = $_POST['title'];
            $data['tag'] = $_POST['tag'];
            $data['content'] = $_POST['content'];
            $data['uptime'] = date('Y-m-d H:i:s',time());
            $add = Db::name('list')->where(array(
                'id'=>$id
            ))->update($data);


            if($add){
                return $this->show(1,'更新成功');

            }else{
                return $this->show(0,'更新失败');
            }

        }

        return $this->fetch();
    }
    /**
     * 删除操作
     */
    public function del(){
        if($_POST){
            $map['id'] = $_POST['id'];
            $res = Db::name('list')->where($map)->delete();
            if($res){
                return $this->show(1,'删除成功');
            }else{
                return $this->show(0,'删除失败');
            }
        }


    }
    /**
     * 获取案例列表
     */
    public function get_list(){
        if (trim($_GET['limit'])) {
            $limit = trim($_GET['limit']);
        }else{
            $limit = 15;
        }
        $pageSize = $limit;
        $page = $_GET['page'];

        $sql = DB::name('list');
        //搜索部分
        $searchParams = json_decode($_GET['searchParams'],true);
        $title = $searchParams['title'];
        $tag = $searchParams['tag'];
        $content = $searchParams['content'];

        if($title){
            $list = $sql
                ->whereOr('title', 'like', '%' . $title . '%')
                ->order('uptime desc')
                ->page($page,$pageSize)
                ->select();
            $count = $sql
                ->whereOr('title', 'like', '%' . $title . '%')
                ->count();
        }
        if($tag){
            $list = $sql
                ->whereOr('tag', 'like', '%' . $tag . '%')
                ->order('uptime desc')
                ->page($page,$pageSize)
                ->select();
            $count = $sql
                ->whereOr('tag', 'like', '%' . $tag . '%')
                ->count();
        }
        if($content){
            $list = $sql
                ->whereOr('content', 'like', '%' . $content . '%')
                ->order('uptime desc')
                ->page($page,$pageSize)
                ->select();
            $count = $sql
                ->whereOr('content', 'like', '%' . $content . '%')
                ->count();
        }

        if(!$title && !$tag && !$content ){
            $list = $sql
                ->order('uptime desc')
                ->page($page,$pageSize)
                ->select();
            $count = $sql
                ->count();
        }


         //计算喜欢数
        for($i=0;$i<count($list);$i++){
            $love = db::name('love')

                ->where(array(
                    'list_id'=>$list[$i]['id']
                ))->count();
            $list[$i]['love'] = $love;

        }
        if($list){
            $data['code'] = '0';
            $data['msg'] = '';
            $data['count'] = $count;
            $data['data'] = $list;
        }else{
            $data['code'] = '0';
            $data['msg'] = '暂无数据';
            $data['count'] = 0;
            $data['data'] = '';
        }

        exit(json_encode($data));
    }


    /**
     * 文件上传函数
     */
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg

        $file = request()->file('myFile');
//            var_dump($file);

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->validate(['size'=>126976000,'ext'=>'jpg,png,gif,jpeg,doc,docx,ppt,pptx,xls,xlsx,pdf,txt,mp4,avi,mov,wmv,zip,rar,7z,exe'])
                ->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息

                $res['path'] = 'https://al.igoodstudy.com/uploads/'.$info->getSaveName();
                $res['name'] = $info->getFilename();
                $data['name'] =  $res['name'];
                $data['path'] =   $res['path'];
                $data['type'] =   $info->getMime();
                $data['uptime'] = date('Y-m-d H:i:s',time());
                $add = Db::name('file')->insert($data);
                $res['id'] = Db::name('file')->getLastInsID();
                $this->show(1,'上传成功',$res);


            }else{
                // 上传失败获取错误信息
                $this->show(0,'获取失败',$file->getError());

            }
        }
    }

    /**
     * 选择标签
     */
    public function add_tag(){
        //        获取标签集合数据
        $list = DB::name('tag_sort')
            ->select();
        for ($i = 0; $i < count($list); $i++) {
            $list[$i]['detail'] = DB::name('tag_detail')
                ->where(array(
                    'tag_id' => $list[$i]['name']
                ))->select();

        }
        $this->assign('list', $list);
        return $this->fetch();
    }
    /**
     * 案例上传
     */
     public function up_list(){
         $db = DB::name('list');
         $data['title'] = $_POST['title'];
         $data['tag'] = $_POST['tag'];
         $data['img'] = $_POST['img'];
         $data['ids'] = $_POST['ids'];
         $data['content'] = $_POST['content'];
         $data['uptime'] = date('Y-m-d H:i:s',time());
         $res = $db->insert($data);
         $anli_id = Db::name('list')->getLastInsID();
//         写入数据表后，更新文件库中的id
         $array = explode(',', $data['ids']);
         $map['anli_id'] = $anli_id;
         for($i=0;$i<count($array);$i++){
             DB::name('file')
                 ->where(array(
                     'id'=>$array[$i]
                 ))
                 ->update($map);
         }


         if($res){
             return show(1,'提交成功');
         }else{
             return show(0,'提交失败，请重试');
         }
     }

   /**
    * 查看案例附件
    */
   public function open_file(){

//       获取项目信息
       $list = DB::name('file')
           ->where(array(
               'anli_id'=>$_GET['id']
           ))
           ->select();

       $this->assign('list', $list);
       return $this->fetch();
   }

}
